import {Toast} from "mint-ui";
import request from "./request";

export default {
    warnFunc: function (res) {
        if (res.code == 301) {
            this.toast(res.msg);
            return true;
        }
        return false;
    },
    toast: (msg, sec) => {
        Toast({
            message: msg, //弹窗内容
            position: "middle", //弹窗位置
            duration: sec || 2000, //弹窗时间毫秒,如果值为-1，则不会消失
        });
    },
    checkMobile: (text) => {
        return /^1[3456789]\d{9}$/.test(text);
    },
    checkPassword: (pwd) => {
        if (pwd.length < 6 || pwd.length > 15) return false;
        let test1 = pwd.replace(/[a-zA-Z]*/g,"");
        if (test1.length == 0 || test1.length == pwd.length) return false;
        let test2 = test1.replace(/[0-9]*/g,"");
        return test2.length == 0;
    },
    async getFromCache(cacheName, ajaxUrl) {
        let cacheStr = localStorage.getItem(cacheName);
        if(!cacheStr){
            let res = await request({}, ajaxUrl);
            if(res && res.code == 0){
                localStorage.setItem(cacheName, JSON.stringify(res.data));
                return res.data;
            }
        }else{
            return JSON.parse(cacheStr);
        }
    }
}

